package designpatterns.observer;

import java.util.Vector;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2022/2/10 8:30
 */
public class Subject {

    /**
     * 定义观察者的集合 使用vector 线程安全
     */
    private final Vector<Observer> observerList = new Vector<>();

    private int state;

    /**
     * 注册观察者
     *
     * @param observer 观察者
     */
    public void registerObserver(Observer observer) {
        observerList.add(observer);
    }

    /**
     * 通知所有的观察者
     */
    public void notifyAllObservers() {
        for (Observer observer : observerList) {
            observer.update();
        }
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        notifyAllObservers();
    }
}
